﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using XRM.Security.DataAccess.Helpers;
using XRM.Security.WebUI.Models.CertRegister;
using XRM.Security.WebUI.Models.IdentityCenter;
using XRM.Security.WebUI.Models.InfoSystem;
using XRM.Security.WebUI.Models.UserRegister;
using XRM.Security.WebUI.Models.WorkKind;
using db = XRM.Security.DataAccess.Models;

namespace XRM.Security.WebUI.Models.Request
{
    public class RequestWorkKindParamModel : BaseViewModel
    {
        public RequestWorkKindParamModel()
        {
            WorkKindParamSpecList = new List<RequestWorkKindParamSpecModel>();
            Cert = new CertRegisterModel();
        }

        public RequestWorkKindParamModel(db.RequestWorkKindParam dbEntity)
        {
            WorkKindParamSpecList = new List<RequestWorkKindParamSpecModel>();
            Cert = new CertRegisterModel();

            if (dbEntity != null)
            {
                Id = dbEntity.RequestWorkKindParamId;
                RequestWorkKindId = dbEntity.RequestWorkKindId;
                WorkKindParamId = dbEntity.WorkKindParamId;
                WorkKindParam = new WorkKindParamModel(dbEntity.WorkKindParam);

                ParamValue = dbEntity.ParamValue;
                ParamValue1 = dbEntity.ParamValue1;
                SkziKindId = dbEntity.SkziKindId;
                PCId = dbEntity.PCId;
                InfoSystemId = dbEntity.InfoSystemId;
                DepartmentId = dbEntity.DepartmentId;
                DepartmentAddressId = dbEntity.DepartmentAddressId;

                if (dbEntity.RequestWorkKindParamSpecs != null && dbEntity.RequestWorkKindParamSpecs.Any())
                {
                    foreach (db.RequestWorkKindParamSpec item in dbEntity.RequestWorkKindParamSpecs)
                    {
                        WorkKindParamSpecList.Add(new RequestWorkKindParamSpecModel(item));
                    }
                }

                if ( dbEntity.RequestCertId.HasValue )
                {
                    RequestCertId = dbEntity.RequestCertId;
                    Cert = new CertRegisterModel( dbEntity.RequestCert );
                }

                /*if (dbEntity.RequestCertId.HasValue)
                {
                    Cert.FileName = dbEntity.RequestCert.FileName;

                    Cert.UserRegisterId = dbEntity.RequestCert.UserRegisterId;
                    Cert.CertUser = new UserRegisterModel(dbEntity.RequestCert.UserRegister);
                    Cert.CertUser.PCList.Insert(0, new PCModel { Id = -1, PCHost = "(Выберите серийный номер ПК)" });
                    
                    Cert.IdentityCenterId = dbEntity.RequestCert.IdentityCenterId;
                    Cert.CertIdentityCenter = new IdentityCenterModel(dbEntity.RequestCert.IdentityCenter);
                    Cert.PCId = dbEntity.RequestCert.PCId;
                    Cert.CertPC = new PCModel(dbEntity.RequestCert.PC);

                    Cert.UploadDate = dbEntity.RequestCert.UploadDate;
                    Cert.FromDate = dbEntity.RequestCert.FromDate;
                    Cert.ToDate = dbEntity.RequestCert.ToDate;
                    Cert.Status = dbEntity.RequestCert.Status();

                    Cert.SerialNumber = dbEntity.RequestCert.SerialNumber;
                    Cert.SerialNumberEtoken = dbEntity.RequestCert.SerialNumberEtoken;
                    Cert.Thumbprint = dbEntity.RequestCert.Thumbprint;

                    if (dbEntity.RequestCert.RequestCertInfoSystems != null && dbEntity.RequestCert.RequestCertInfoSystems.Any())
                    {
                        foreach (db.RequestCertInfoSystem item in dbEntity.RequestCert.RequestCertInfoSystems)
                        {
                            Cert.InfoSystemList.Add(new InfoSystemModel(item.InfoSystem));
                        }
                    }
                }*/
            }
        }

        public RequestWorkKindParamModel(db.WorkKindSpec dbEntity)
        {
            if (dbEntity != null)
            {
                Id = -1;
                WorkKindParamId = dbEntity.WorkKindParamId;
                WorkKindParam = new WorkKindParamModel(dbEntity.WorkKindParam);
            }
        }

        public void GetWorkKindParam(db.WorkKindParam workKindParam)
        {
            WorkKindParam = new WorkKindParamModel(workKindParam);
        }
        
        public int RequestWorkKindId { get; set; }
        public int WorkKindParamId { get; set; }

        public string ParamValue { get; set; }
        public string ParamValue1 { get; set; }

        public int? SkziKindId { get; set; }
        public int? PCId { get; set; }
        public int? InfoSystemId { get; set; }
        
        [Display(Name = "Структурное подразделение")]
        public int? DepartmentId { get; set; }
        [Display(Name = "Адрес")]
        public int? DepartmentAddressId { get; set; }

        public int? RequestCertId { get; set; }
        public CertRegisterModel Cert { get; set; }
        public string DataId { get; set; }

        public WorkKindParamModel WorkKindParam { get; set; }
        public List<RequestWorkKindParamSpecModel> WorkKindParamSpecList { get; set; }
    
    }
}